/**
 * @author: Loy-winters1224@163.com
 * @date: 2014-8-21-下午4:06:14
 */
package net.loyintean.pearlshell.wechat.messagepasser.impl;

import org.dom4j.Document;
import org.dom4j.Element;

import net.loyintean.pearlshell.wechat.common.EventEnum;
import net.loyintean.pearlshell.wechat.messagepasser.AbstractMessageBuilder;
import net.loyintean.pearlshell.wechat.model.BaseMessageModel;
import net.loyintean.pearlshell.wechat.model.event.EventMessageModel;

/**
 * build a subscribe/unsubscribe event message
 * 
 * @author Loy-winters1224@163.com
 * @version 2014-8-21.1.0.0
 */
public class EventMessageBuilder extends AbstractMessageBuilder {

	private EventMessageModel message = new EventMessageModel();

	/**
	 * 
	 * @author Loy-winters1224@163.com
	 * @see net.loyintean.pearlshell.wechat.messagepasser.AbstractMessageBuilder
	 *      #getMessage(org.dom4j.Document)
	 * 
	 */
	@Override
	public BaseMessageModel getMessage(Document document) {
		assert document != null;

		document.accept(this);
		return message;
	}

	@Override
	public void visit(Element element) {
		assert element != null;

		if ("TOUSERNAME".equals(element.getName().toUpperCase())) {
			message.setToUserName(element.getText());
		}

		if ("FROMUSERNAME".equals(element.getName().toUpperCase())) {
			message.setFromUserName(element.getText());
		}

		if ("CREATETIME".equals(element.getName().toUpperCase())) {
			message.setCreateTime(Integer.parseInt(element.getText()));
		}

		if ("EVENT".equals(element.getName().toUpperCase())) {
			message.setEvent(EventEnum.valueOf(element.getText()));
		}

	}
}
